package cn.yuanqiao.web.controller.archive.liyong;

/**
 * @author zjj
 * @date 2023/9/12
 */

import cn.yuanqiao.archive.liyong.domain.vo.DownloadRecord;
import cn.yuanqiao.archive.liyong.service.TenantDownloadRecordSerive;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/archive/downloadRecord")
public class TenantDownloadRecordController extends BaseController {
    @Autowired
    private TenantDownloadRecordSerive downloadRecordSerive;

    @Log(title = "下载记录", businessType = BusinessType.SELECT)
    @PostMapping("/selectDownloadRecord")
    public TableDataInfo selectDownloadRecord(@RequestBody JSONObject jsonObject){
        Long categoryId = null;
        if(jsonObject.containsKey("categoryId") && StringUtils.isNotEmpty(jsonObject.getString("categoryId"))){
            categoryId = jsonObject.getLong("categoryId");
        }
        Long menuId = null;
        if(jsonObject.containsKey("menuId") && StringUtils.isNotEmpty(jsonObject.getString("menuId"))){
            menuId = jsonObject.getLong("menuId");
        }
        String fileName = null;
        if(jsonObject.containsKey("fileName") && StringUtils.isNotEmpty(jsonObject.getString("fileName"))){
            fileName = jsonObject.getString("fileName");
        }
        String userName = null;
        if(jsonObject.containsKey("userName") && StringUtils.isNotEmpty(jsonObject.getString("userName"))){
            userName = jsonObject.getString("userName");
        }

        List<DownloadRecord> list = downloadRecordSerive.selectDownloadRecord(categoryId,menuId,fileName,userName);
        return getDataTable(list);
    }

    @Log(title = "下载按钮", businessType = BusinessType.DOWNLOAD)
    @PostMapping("/download")
    public void download(@RequestParam("id")Long id,@RequestParam("sydm")String sydm,HttpServletResponse response)throws Exception{
        if ("null".equals(sydm)){
            sydm = "";
        }
        downloadRecordSerive.download(id,sydm,response);
    }
}
